#ifndef SQL_MANAGER_H
#define SQL_MANAGER_H

#include <QString>
#include <QStringList>
#include <QSqlDatabase>

#include "BlackBear.h"
#include "BBObject.h"

class BBTable;

class SqlManager
{
public:
	static SqlManager* instance();

	BlackBear* connect( const QString& dataBaseName,
						const QString& userName,
						const QString& password,
						const QString& hostName = 0);

	bool createDataBase( const QString& dataBaseName,
						 const QString& userName,
						 const QString& password,
						 const QString& hostName = 0 );

	bool createTable( BlackBear& blackBear, BBTable& table );

	bool insertRow( BlackBear& blackBear, BBObject& object );

	bool updateRow( BlackBear& blackBear, BBObject& object );

	bool deleteRow( BlackBear& blackBear, BBObject& object );

	bool selectRow( BlackBear& blackBear, BBObject& object );
	
	int countRows( BlackBear& blackBear, QString& tableName );

	QVector<int> getIds( BlackBear& blackBear, QString& tableName );

	QStringList getConnectionsName( BlackBear& blackBear )
	{
		return blackBear.m_dataBase->connectionNames();
	}

protected:
	SqlManager()
	{
		//DOES NOTHING
	}
	~SqlManager()
	{
		//DOES NOTHING
	}	

	static int getConnectionNumber()
	{
		return m_connectionNumber++;
	}

	QString getNewConnectionName();

	QString getSQLVariableType( const QString& type );

	int getLastIdFromTable( BlackBear& blackBear, BBObject& object );

	QString convertToSQLFormat( const QString type, const QVariant value );

	//Atençao: Talvez este método seja necessario quando utilizar diversos banco de dados 
	//QString buildAttributeValueStringByType( QVariant value, QString type );

private:
	static SqlManager* m_instance;
	static int m_connectionNumber;
};

#endif